﻿<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript" language="javascript">
    var pageNum;
    var ob;
    var BtnID;
    $(document).ready(function () {
        //$("#btnSub").click(function () {
            //$.ajax({
            //    type: "POST",
            //    url: "/Home/test1",//地址
            //    //data: "id=" + txt1.value,//后台有一个id接收数据
            //    success: function (num) {
            //        pageNum = num;
            //    }
            //});
        //});
        //按钮排列如：1234...n
        //1...456...n
        //1...n-3 n-2 n-1 n
        $.ajax({
            async:false,//不加这个下面的js不会自己执行
            type: "POST",
            url: "/Home/test1",//地址
            success: function (num) {
                pageNum = num;
            }
        });
        $.ajax({
            async: false,//不加这个下面的js不会自己执行
            type: "POST",
            url: "/Home/test2",//地址
            success: function (FristPage) {
                var obj = eval(FristPage);
                for (var i = 0; i < 5; i++) {//5可以随意定
                    var pageID = "txt" + "" + i + "";
                    var divMain = document.getElementById("divMain");
                    var pagediv = document.createElement("div");
                    pagediv.innerHTML = "<label id=" + pageID + "></label>";
                    divMain.appendChild(pagediv);
                    $("#" + pageID + "").text(obj[i]);
                }
            }
        });
        Init();
    });
    //我自己都不想看自己代码了
    function Init() {
        if (pageNum > 5) {
            for (var count = 0; count < 7; count++) {
                if (count < 5) {
                    var page = count + 1;
                    var dMain = document.getElementById("dMain");
                    var pageDiv = document.createElement("div");
                    pageDiv.style.float = "left";
                    pageDiv.innerHTML = "<input type='button' id=" + page + " value=" + page + " onclick='CliBtn(this)'>";
                    dMain.appendChild(pageDiv);
                }
                else {
                    if (count == 5) {
                        var page = count + 1;
                        var dMain = document.getElementById("dMain");
                        var pageDiv = document.createElement("div");
                        pageDiv.style.float = "left";
                        pageDiv.innerHTML = "...";
                        dMain.appendChild(pageDiv);
                    }
                    if (count == 6) {
                        var page = count + 1;
                        var dMain = document.getElementById("dMain");
                        var pageDiv = document.createElement("div");
                        pageDiv.style.float = "left";
                        pageDiv.innerHTML = "<input type='button' id=" + pageNum + " value=" + pageNum + " onclick='CliBtn(this)'>";;
                        dMain.appendChild(pageDiv);
                    }
                }
            }
        }
        else {
            for (var count = 0; count < pageNum; count++) {
                var page = count + 1;
                var dMain = document.getElementById("dMain");
                var pageDiv = document.createElement("div");
                pageDiv.style.float = "left";
                pageDiv.innerHTML = "<input type='button' id=" + page + " value=" + page + " onclick='CliBtn(this)'>";
                dMain.appendChild(pageDiv);
            }
        }
        BtnID = 1;
    }
    function CliBtn(btn) {
        //////ajax部分用来获取文本
        $.ajax({
            async: false,//不加这个下面的js不会自己执行
            type: "POST",
            url: "/Home/test3",//地址
            data: "id=" + btn.id,//后台有一个id接收数据
            success: function (pageText) {
                ob = eval(pageText);
                for (var count = 0; ob[count]; count++);
                var tempCount = 0;
                for (var j = 0; j < 5; j++)//这个5对应初始化txt时的5
                {
                    pID = "txt" + "" + j + "";
                    if (tempCount < count) {
                        $("#" + pID + "").text(ob[j]);
                        tempCount++;
                    }
                    else {
                        $("#" + pID + "").text("");
                    }
                }
            }
        })
        Change(btn.id);
        getTempBtn(btn.id);
    }
    function getTempBtn(btnID) {
        BtnID = btnID;
    }
    function Change(num) {
        //假设有n页
        var counti = 0;
        /////////////////////////////////////新加这段代码//////////////////////////////////////////
        if (num <= 4) {
            $("#dMain").replaceWith("<div id='dMain' style='float:left'></div>");
            for (var count = 0; count < 7; count++) {
                if (count < 5) {
                    var page = count + 1;
                    var dMain = document.getElementById("dMain");
                    var pageDiv = document.createElement("div");
                    pageDiv.style.float = "left";
                    pageDiv.innerHTML = "<input type='button' id=" + page + " value=" + page + " onclick='CliBtn(this)'>";
                    dMain.appendChild(pageDiv);
                }
                else {
                    if (count == 5) {
                        var page = count + 1;
                        var dMain = document.getElementById("dMain");
                        var pageDiv = document.createElement("div");
                        pageDiv.style.float = "left";
                        pageDiv.innerHTML = "...";
                        dMain.appendChild(pageDiv);
                    }
                    if (count == 6) {
                        var page = count + 1;
                        var dMain = document.getElementById("dMain");
                        var pageDiv = document.createElement("div");
                        pageDiv.style.float = "left";
                        pageDiv.innerHTML = "<input type='button' id=" + pageNum + " value=" + pageNum + " onclick='CliBtn(this)'>";
                        dMain.appendChild(pageDiv);
                    }
                }
            }
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////
        if (num > 4 && num <= pageNum - 2) {
            $("#dMain").replaceWith("<div id='dMain' style='float:left'></div>");
            var BtnArray = new Array();
            for (var i = Number(num) - 2;i<Number(num)+3; i++) {
                BtnArray.push("<input type='button' id=" + i + " value=" + i + " onclick='CliBtn(this)'>");
                counti++;
            }
            $("#dMain").append("<input type='button' id='1' value='1' onclick='CliBtn(this)' style='float:left'>");
            $("#dMain").append("<div style='float:left;'>...</div>");
            for (var cci = 0; cci < counti; cci++) {
                var divMain = document.getElementById("dMain");
                var pa = document.createElement("div");
                pa.style.float = "left";
                pa.innerHTML = BtnArray[cci];
                divMain.appendChild(pa);
            }
            $("#dMain").append("<div style='float:left'>...</div>");
            $("#dMain").append("<input type='button' id='" + pageNum + "' value='" + pageNum + "' onclick='CliBtn(this)' style='float:left'>");
        }
        if (num > pageNum - 2) {
            $("#dMain").replaceWith("<div id='dMain' style='float:left' ></div>");
            var btnArray = new Array();
            for (var ii = pageNum - 3; ii <=pageNum; ii++) {
                btnArray.push("<input type='button' id=" + ii + " value=" + ii + " onclick='CliBtn(this)'>");
                counti++;
            }
            $("#dMain").append("<input type='button' id='1' value='1' onclick='CliBtn(this)' style='float:left'>");
            $("#dMain").append("<div style='float:left'>...</div>");
            for (var cci = 0; cci < counti; cci++) {
                var divMain = document.getElementById("dMain");
                var pa = document.createElement("div");
                pa.style.float = "left";
                pa.innerHTML = ""+btnArray[cci]+"";
                divMain.appendChild(pa);
            }
        }
    }
    function NextPage() {
        if (Number(BtnID) != pageNum) {
            var temp = Number(BtnID) + 1;
            $.ajax({
                async: false,//不加这个下面的js不会自己执行
                type: "POST",
                url: "/Home/test3",//地址
                data: "id=" + temp,//后台有一个id接收数据
                success: function (pageText) {
                    ob = eval(pageText);
                    for (var count = 0; ob[count]; count++);
                    var tempCount = 0;
                    for (var j = 0; j < 5; j++)//这个5对应初始化txt时的5
                    {
                        pID = "txt" + "" + j + "";
                        if (tempCount < count) {
                            $("#" + pID + "").text(ob[j]);
                            tempCount++;
                        }
                        else {
                            $("#" + pID + "").text("");
                        }
                    }
                }
            })
            Change(temp);
            BtnID = temp;
        }
    }
    function PriorPage() {
        if (Number(BtnID) != 1) {
            var temp = Number(BtnID) - 1;
            $.ajax({
                async: false,//不加这个下面的js不会自己执行
                type: "POST",
                url: "/Home/test3",//地址
                data: "id=" + temp,//后台有一个id接收数据
                success: function (pageText) {
                    ob = eval(pageText);
                    for (var count = 0; ob[count]; count++);
                    var tempCount = 0;
                    for (var j = 0; j < 5; j++)//这个5对应初始化txt时的5
                    {
                        pID = "txt" + "" + j + "";
                        if (tempCount < count) {
                            $("#" + pID + "").text(ob[j]);
                            tempCount++;
                        }
                        else {
                            $("#" + pID + "").text("");
                        }
                    }
                }
            })
            Change(temp);
            BtnID = temp;
        }
    }
</script>
<body>
    <div id="BiggestDiv" style="height:200px;">
        
        <div id="divMain">
        </div>
    </div>
    <input type="button" value="上一页" style="float:left" onclick="PriorPage()" />
    <div id="dMain" style="float:left">
    </div>
    <input type="button" value="下一页" style="float:left" onclick="NextPage()" />
</body>